﻿#region copyright
/*
 * Author: Jan Vilimek: jan (at) vilimek (dot) cz
 * If you use any code from this program/library, please include above line
 * 
 */
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace pdb2mobiGUI
{
	static class Utils
	{
		public static UInt32 ConvertBigEndianUInt32(byte[] buffer, int startOffset)
		{
			const int size = 4;
			var newBuffer = new byte[size] { 0, 0, 0, 0 };
			for (int i = startOffset, j = size - 1; i < buffer.Length && j >= 0; i++, j--)
			{
				newBuffer[j] = buffer[i];
			}
			return BitConverter.ToUInt32(newBuffer, 0);
		}
		public static UInt32 ConvertBigEndianUInt32(byte[] buffer, ref int startOffset)
		{
			var ret = ConvertBigEndianUInt32(buffer, startOffset);
			startOffset += 4;
			return ret;
		}

		public static UInt16 ConvertBigEndianUInt16(byte[] buffer, int startOffset)
		{
			const int size = 2;
			var newBuffer = new byte[size] { 0, 0 };
			for (int i = startOffset, j = size - 1; i < buffer.Length && j >= 0; i++, j--)
			{
				newBuffer[j] = buffer[i];
			}
			return BitConverter.ToUInt16(newBuffer, 0);
		}
		public static UInt16 ConvertBigEndianUInt16(byte[] buffer, ref int startOffset)
		{
			var ret = ConvertBigEndianUInt16(buffer, startOffset);
			startOffset += 2;
			return ret;
		}
		public static void Copy(byte[] from, byte[] to, int start, int count)
		{
			for (int i = start, j = 0; i < start + count && j < to.Length; i++, j++)
				to[j] = from[i];
		}
		public static void Copy(byte[] from, byte[] to, ref int start, int count)
		{
			Copy(from, to, start, count);
			start += count;
		}
	}
}
